package cz.matasek.dbLogger.gui;

import cz.matasek.dbLogger.database.DatabaseObjectType;
import java.util.Arrays;
import java.util.Map;
import javax.swing.table.AbstractTableModel;

/**
 * The class provides table model for listing columns of any database table.
 */
public class LoggingObjectsTableModel extends AbstractTableModel {

    /**
     * Serial version UID.
     */
    private static final long serialVersionUID = 5989155958197835789L;
    /**
     * Column headings.
     */
    private String[] columnHeadings = {"Object name", "Object type"};
    /**
     * Object names.
     */
    private String[] objectNames;
    /**
     * Object types.
     */
    private DatabaseObjectType[] objectTypes;

    /**
     * Creates a new <code>ColumnsTableModel</code> for specified table.
     *
     * @param objects logger objects
     */
    public LoggingObjectsTableModel(Map<String, DatabaseObjectType> objects) {
        objectNames = objects.keySet().toArray(new String[1]);
        objectTypes = new DatabaseObjectType[objectNames.length];

        Arrays.sort(objectNames);
        for (int i = 0; i < objectNames.length; ++i) {
            objectTypes[i] = objects.get(objectNames[i]);
        }
    }

    /**
     * Returns the number of rows in the model.
     *
     * @return the number of rows in the model
     */
    @Override
    public int getRowCount() {
        return objectNames.length;
    }

    /**
     * Returns the number of columns in the model.
     *
     * @return the number of columns in the model
     */
    @Override
    public int getColumnCount() {
        return columnHeadings.length;
    }

    /**
     * Returns the name of the column at <code>columnIndex</code>.
     *
     * @param columnIndex the index of the column
     * @return the name of the column
     */
    @Override
    public String getColumnName(int columnIndex) {
        return columnHeadings[columnIndex];
    }

    /**
     * Returns the most specific superclass for all the cell values in the
     * column.
     *
     * @param columnIndex the index of the column
     * @return the common ancestor class of the object values in the model
     */
    @Override
    public Class<?> getColumnClass(int columnIndex) {
        switch (columnIndex) {
            case 0:
            case 1:
                return String.class;
            default:
                assert false;
                return Object.class;
        }
    }

    /**
     * Returns <code>true</code> if the cell at <code>rowIndex</code> and
     * <code>columnIndex</code> is editable.
     *
     * @param rowIndex the row whose value to be queried
     * @param columnIndex the column whose value to be queried
     * @return <code>true</code> if the cell is editable; <code>false</code>
     * otherwise
     */
    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return false;
    }

    /**
     * Returns the value for the cell at <code>rowIndex</code> and
     * <code>columnIndex</code>.
     *
     * @param rowIndex the row whose value to be queried
     * @param columnIndex the column whose value to be queried
     * @return the value Object at the specified cell
     */
    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        switch (columnIndex) {
            case 0:
                return objectNames[rowIndex];
            case 1:
                return objectTypes[rowIndex];
            default:
                assert false;
                return null;
        }
    }
}
